/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m005.bl;

import com.vaadin.data.util.BeanItem;
import ep.m005.ui.controllers.M005Controller;
import ep.persistence.controllers.T006PrescriptionJpaController;
import ep.persistence.controllers.T008DosageJpaController;
import ep.persistence.controllers.T009MedicineJpaController;
import ep.persistence.controllers.T012DiagnosisJpaController;
import ep.persistence.controllers.T013FrequencyJpaController;
import ep.persistence.entities.SavePrescriptionBean;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T006Prescription;
import ep.persistence.entities.T008Dosage;
import ep.persistence.entities.T009Medicine;
import ep.persistence.entities.T012Diagnosis;
import ep.persistence.entities.T013Frequency;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Spyros
 */
@Path("bl005")
public class BL005Controller {
    
    @GET
    @Path("/medsub")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T009Medicine> getMedicineBySubstance(@QueryParam("substance") String substance) {
        String queryString = "select distinct m.* "
                + "from t009_medicine m "
                + "inner join t009_t010 tt on tt.t009_medicine_id = m.id "
                + "inner join t010_substance s on s.id = tt.t010_substance_id "
                + "where s.name like '%" + substance + "%' ";
        T009MedicineJpaController jpaController = new T009MedicineJpaController();
        Query query = jpaController.getEntityManager().createNativeQuery(queryString, T009Medicine.class);
//        query.setParameter(1, substance);
        return query.getResultList();
    }
    
    @GET
    @Path("/medname")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T009Medicine> getMedicineByMedName(@QueryParam("medname") String medName) {
        String queryString = "SELECT * FROM t009_medicine t WHERE t.name like '%" + medName + "%'";
        T009MedicineJpaController jpaController = new T009MedicineJpaController();
        Query query = jpaController.getEntityManager().createNativeQuery(queryString, T009Medicine.class);
        return query.getResultList();
    }
    
    @POST
    @Path("/spresc")
//    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String savePrescriptionDosage(SavePrescriptionBean bean) {
        T002Doctor doctor = bean.getDoctor();
        T005Patient patient = bean.getPatient();
        T006Prescription prescription = bean.getPrescription();
        Collection dosages = bean.getDosages();
        boolean repeat = bean.isRepeat();
        try {
            //save prescription
            T006PrescriptionJpaController prescriptionJpaController = new T006PrescriptionJpaController();
            prescription.setT002Doctor(doctor);
            prescription.setT005Patient(patient);
            prescription.setPrescDate(new Date());
            if (prescription.getId() == null) {
                prescriptionJpaController.create(prescription);
            } else {
                if (repeat) {
                    prescription.setT008DosageCollection(new ArrayList<T008Dosage>());
                    prescriptionJpaController.create(prescription);
                } else {
                    T006Prescription oldPrescription = prescriptionJpaController.findT006Prescription(prescription.getId());
                    prescriptionJpaController.edit(oldPrescription);
                }
            }
            //save dosage
            T008DosageJpaController dosageJpaController = new T008DosageJpaController();
            for (Iterator it = dosages.iterator(); it.hasNext();) {
                T008Dosage dosage = (T008Dosage) it.next();
                if (repeat) {
                    dosage.setPrescribed(false);
                }
                dosage.setT006Prescription(prescription);
                if (dosage.getId() == null) {
                    dosageJpaController.create(dosage);
                } else {
                    if (repeat) {
                        dosageJpaController.create(dosage);
                    } else {
                        T008Dosage old = dosageJpaController.findT008Dosage(dosage.getId());
                        old.setPrescribed(dosage.getPrescribed());
                        dosageJpaController.edit(old);
                    }
                }
            }
//            prescription.setT008DosageCollection(dosages);
//            prescriptionJpaController.edit(prescription);

            return "0";
        } catch (Exception e) {
            Logger.getLogger(M005Controller.class.getName()).log(Level.SEVERE, null, e);
            return "-1";
        }
    }
    
    @GET
    @Path("/prescription")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_JSON})
    public T006Prescription findPrescription(@QueryParam("id") String id) {
        T006PrescriptionJpaController jpaController = new T006PrescriptionJpaController();
        return jpaController.findT006Prescription(Integer.parseInt(id));
    }
    
    @GET
    @Path("/medicine")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T009Medicine> getMedicineCollection() {
        T009MedicineJpaController jpaController = new T009MedicineJpaController();
        return jpaController.findT009MedicineEntities();
    }
    
    @GET
    @Path("/diagnosis")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T012Diagnosis> getDiagnosisCollection() {
        T012DiagnosisJpaController jpaController = new T012DiagnosisJpaController();
        return jpaController.findT012DiagnosisEntities();
    }
    
    @GET
    @Path("/frequency")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T013Frequency> getFrequencyCollection() {
        T013FrequencyJpaController jpaController = new T013FrequencyJpaController();
        return jpaController.findT013FrequencyEntities();
    }
}
